package com.xingchi.tornado.dict.enums;

import com.xingchi.tornado.anno.Dict;
import com.xingchi.tornado.basic.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 字段数据来源
 *
 * @author 17105
 * @date 2025/5/27 15:47
 */
@Getter
@Dict(value = "DICT_SOURCE", name = "字典来源", desc = "字典来源信息")
public enum DictSourceEnum implements BaseEnum {

    SYSTEM_ADD(1, "系统添加"),
    ENUM_SCAN(2, "枚举扫描"),
    ;
    private final Integer code;
    private final String desc;

    DictSourceEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    private static final Map<Integer, DictSourceEnum> CODE_MAPPINGS = Arrays.stream(DictSourceEnum.values())
            .collect(Collectors.toMap(DictSourceEnum::getCode, Function.identity()));

    public static DictSourceEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static DictSourceEnum getInstanceOrDefault(Integer code, DictSourceEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

    public static boolean exists(Integer code) {
        return CODE_MAPPINGS.containsKey(code);
    }

}
